<?php
namespace app\admin\controller;

use app\model\model\Admin_menu;
use app\model\model\Contact;
use app\model\model\Free_tags;
use app\model\model\Product_type;
use think\Request;

class Index extends BaseController
{
    protected $_Admin_menu;
    protected $_Adminuser;
    protected $_Product;
    protected $_News;
    protected $_Video;
    protected $_Message;
    protected $Pv;
    protected $_Seo;
    protected $_Ad;
    protected $_Freetags;
    protected $_Page;
    protected $_Setup;
    protected $_Social;
    protected $_Comm;
    protected $_Nav;
    protected $_Producttype;
    protected $_product;
    protected $_Knowledge;
    
    public function initialize(){
        $this -> _initialize();
        $this->_Admin_menu = new Admin_menu();
        $this->_Adminuser = new \app\model\model\Adminuser();
        $this->_Product = new \app\model\model\Product();
        $this->_News = new \app\model\model\News();
        $this->_Video = new \app\model\model\Video();
        $this->Pv = new Statistics();
        $this->_Message = new Contact();
        $this->_Ad = new Ad();
        $this->_Page = new \app\model\model\Page();
        $this->_product = new \app\model\model\Product();
        $this->_Freetags = new Free_tags();
       // $this->_Setup = new Setup();
        $this->_Producttype = new Product_type();
        $this->_News = new \app\model\model\News();
        $this->_Video = new \app\model\model\Video();
        $this->_Knowledge = new \app\model\model\News();
        $this->_Nav = new \app\model\model\Nav();
    }
    
    public function index()
    {
        //后台菜单
        $data1 = $this->_Admin_menu->getMenuListRankOne(1);
        $data2 = $this->_Admin_menu->getMenuListRankOne(2);
        $data3 = $this->_Admin_menu->getMenuListRankOne(3);

        $this->assign('menulist1',$data1);
        $this->assign('menulist2',$data2);
        $this->assign('menulist3',$data3);
        $this->assign('auth_menu', $this->_getAuthMenu());

        return $this->fetch();
    }
    public function index1(){
        //产品数
        $product = $this->_Product->getCount(['flag'=>1]);
        $this->assign('product',$product);
        //新闻数
        $news = $this->_News->getCount(['flag'=>1]);
        $this->assign('news',$news);
        //视频数
        $video = $this->_Video->getCount(['flag'=>1]);
        $this->assign('video',$video);

        //访问数
        $this->assign('params', $this->Pv->_statisticsPv());
        //询盘数
        $mess = $this->_Message->getListCount();
        $this->assign('mess',$mess);
        return $this->fetch();
    }
    //修改密码
    public function change(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{
            $user = $this->_Adminuser->getUserOne(session('admin_id'));
            if(md5(input('password'))!=$user['password']){
                return json_echo_msg(500,"原密码错误！");
            }
            $data = [
                'password'=>md5(input('passwordnew'))
            ];
            $re = $this->_Adminuser->updateOneById(session('admin_id'),$data);
            if($re){
                return json_echo_msg(200,'修改成功！');
            }else{
                return json_echo_msg(300,'修改失败！请联系技术人员');
            }
        }
    }
    //清楚缓存
    public function clear_cache(){
      $re = deldir(TEMP_PATH );
      if ($re==200){
          return json_echo_msg(200,'清除成功！');
      }else{
          return json_echo_msg(201,'清除失败！');
      }

    }
    public function ditu(){

        $blog = $this->_News->getListHome(['flag'=>1,'classify'=>2]);
        $news = $this->_News->getListHome(['flag'=>1,'classify'=>1]);
        $nav = $this->_Nav->getListHome(['type'=>24,'flag'=>1]);
        $uplist = $this->_Knowledge->getListSelect(['classify'=>3,'flag'=>1],true);
        $ptype = $this->_Producttype->getListSelect(true);
        $product = $this->_product->getListSelect(['flag'=>1],true);
        $video = $this->_Video->getListHome(['flag'=>1]);
        $time = date('Y-m-d',time());
        $s = "<?xml version='1.0' encoding='utf-8'?>\r\n";
        $s .= "<urlset>\r\n";
        foreach ($blog as $v) {
            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/blog/{$v['id']}.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";
        }
        foreach ($news as $v) {
            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/news/{$v['id']}.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";
        }
        foreach ($video as $v) {
            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/video/{$v['id']}.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";
        }
        foreach ($nav as $v) {
            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn{$v['url']}]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";
        }
        foreach ($uplist as $v) {
            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/Upgrades/{$v['id']}.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";
        }
        foreach ($ptype as $v) {
            if($v['id']!=12){
                $s .= "\t\t<url>\n";
                $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/Product-Category/{$v['id']}.html]]></loc>\r\n";
                $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
                $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
                $s .= "\t\t</url>\n";
            }
        }
        foreach ($product as $v) {
            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/product/{$v['id']}.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";
        }
            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/Product-Category.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";

            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/Contact-us.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";

            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/sample.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";

            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/FAQ.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";

            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/Download.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";

            $s .= "\t\t<url>\n";
            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/TERMS.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";$s .= "\t\t<url>\n";

            $s .= "\t\t\t<loc><![CDATA[http://www.oreelaser.cn/makeasample.html]]></loc>\r\n";
            $s .= "\t\t\t<lastmod>{$time}</lastmod>\r\n";
            $s .= "\t\t\t<changefreq>weekly</changefreq>\r\n";
            $s .= "\t\t</url>\n";


        $s .= "\t</urlset>";
        $re = file_exists(ROOT_PATH.'/public/sitemap.xml');
        if($re){
            unlink(ROOT_PATH.'/public/sitemap.xml');
        }
        file_put_contents(ROOT_PATH.'/public/sitemap.xml',$s);
        return json_echo_msg(200,'成功！');

    }
    public function imgUp(){
       return imgup('img');
    }
    public function zipUp(){
        return zipup('zip');
    }
    public function uploadZip(){
        return uploadZip('zip');
    }
    public function videoUp(){
        return videoup('video');
    }

}
